package com.lipiao.smarthome;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Explode;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.lipiao.smarthome.TimePick.ViewDialogFragment;
import com.lipiao.smarthome.Utils.InforUtil;
import com.lipiao.smarthome.fragments.ControlFragment;
import com.lipiao.smarthome.fragments.SetFragment;
import com.lipiao.smarthome.fragments.StatusFragment;
import com.lipiao.smarthome.fragments.UserFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
        implements BottomNavigationBar.OnTabSelectedListener,
        ViewPager.OnPageChangeListener
//        ViewDialogFragment.Callback
{
    public static String getName() {
        return name;
    }

    public static String getPass() {
        return pass;
    }

    static String name="";
    static String pass="";
    BottomNavigationBar mBottomNavigationBar;
    TextBadgeItem mBadgeItem;
    TextBadgeItem mBadgeItem2;
    TextBadgeItem mBadgeItem3;
    TextBadgeItem mBadgeItem4;
    ViewPager viewPager;
    private List<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent=getIntent();
        name=intent.getStringExtra("etUsername");
         pass=intent.getStringExtra("etPassword");

        Explode explode = new Explode();
        explode.setDuration(500);
        getWindow().setExitTransition(explode);
        getWindow().setEnterTransition(explode);
        initView();//初始化视图相关
    }

    public void initView() {
        initBottomNavigationBar();//初始化底部工具栏
        initViewPager();//初始化Viewpager
    }

    public void initViewPager(){
        viewPager = findViewById(R.id.view_pager);

        fragments = new ArrayList<Fragment>();
        fragments.add(new ControlFragment());
        fragments.add(new StatusFragment());
        fragments.add(new SetFragment());
        fragments.add(new UserFragment());

        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);
    }
    public void initBottomNavigationBar(){

        //item1小红点初始化
        mBadgeItem = new TextBadgeItem()
                .setBorderWidth(4)
                .setAnimationDuration(200)
                .setBackgroundColorResource(R.color.colorTab)
                .setHideOnSelect(false)
                .setText("1");
        //item2小红点初始化
        mBadgeItem2 = new TextBadgeItem()
                .setBorderWidth(4)
                .setAnimationDuration(200)
                .setBackgroundColorResource(R.color.colorTab)
                .setHideOnSelect(false)
                .setText("2");
        //item3小红点初始化
        mBadgeItem3 = new TextBadgeItem()
                .setBorderWidth(4)
                .setAnimationDuration(200)
                .setBackgroundColorResource(R.color.colorTab)
                .setHideOnSelect(false)
                .setText("3");
        //item3小红点初始化
        mBadgeItem4 = new TextBadgeItem()
                .setBorderWidth(4)
                .setAnimationDuration(200)
                .setBackgroundColorResource(R.color.colorTab)
                .setHideOnSelect(false)
                .setText("4");
        mBottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.icons8_control_48px, "控制")
                        .setActiveColorResource(R.color.colorBlue)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem))
                .addItem(new BottomNavigationItem(R.drawable.icons8_status_48px, "状态")
                        .setActiveColorResource(R.color.colorBlue)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem2))
                .addItem(new BottomNavigationItem(R.drawable.icons8_settings_48px, "设置")
                        .setActiveColorResource(R.color.colorBlue)
                        .setBadgeItem(mBadgeItem3)
                        .setInActiveColor(R.color.colorTab))
                .addItem(new BottomNavigationItem(R.drawable.icons8_user_48px, "用户")
                        .setActiveColorResource(R.color.colorBlue)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem4))//依次添加item,分别icon和名称
                .setFirstSelectedPosition(0)//设置默认选择item
                .setBarBackgroundColor(R.color.colorTabBack)
                .setTabSelectedListener(this)
                .initialise();//初始化
    }

    //BottomNavigationBar.OnTabSelectedListener接口重载
    @Override
    public void onTabSelected(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {
    }

    @Override
    public void onTabReselected(int position) {

    }


    //ViewPager.OnPageChangeListener接口重载
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        mBottomNavigationBar.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

//    @Override
//    public void onClick(String userName, String password) {
//
//    }
}
